package com.eastmoney.emis.edda.domain.entity;

import com.eastmoney.emis.edda.domain.service.edda.EddaBindCommand;
import com.eastmoney.emis.edda.domain.valueobject.EddaBindStatus;
import com.eastmoney.emis.edda.infrastructure.edda.po.EmhkEddaBindPo;
import org.springframework.web.servlet.support.BindStatus;

import java.time.LocalDate;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain.entity
 * @author: 汪程程
 * @date: 2022/9/30 1:50 PM
 * @version: 1.0
 */
public class EddaBind {

    private String eid;
    private String status;
    private String clientId;
    private String bankCode;
    private String bankAccount;
    private String customEngName;
    private String registerId;
    private String mandateId;
    private LocalDate grantStartDate;
    private LocalDate grantExpiryDate;
    private String creditReference;
    private String note;

    public EddaBind() {
    }

    public EddaBind(EmhkEddaBindPo bindPo) {
        this.eid = bindPo.EID;
        this.status = bindPo.STATUS;
        this.clientId = bindPo.HKCUACCID;
        this.bankCode = bindPo.BANKCODE;
        this.bankAccount = bindPo.BANKACCOUNT;
        this.customEngName = bindPo.CUSTENGNAME;
        this.registerId = bindPo.REGISTID;
        this.mandateId = bindPo.MANDATEID;
        this.grantStartDate = bindPo.GRANTSTARTDATE;
        this.grantExpiryDate = bindPo.GRANTEXPIRYDATE;
        this.creditReference = bindPo.CREDITREFERENCE;
        this.note = bindPo.NOTE;
    }

    public EddaBind(EddaBindCommand command) {
        this.status = command.isHsbc() ? EddaBindStatus.INIT.value() : EddaBindStatus.PROCESSING.value();
        this.clientId = command.getClientId();
        this.bankCode = command.getBankCode();
        this.bankAccount = command.getBankAccount();
        this.customEngName = command.getCustomEngName();
    }

    public void merge(EddaBindCommand command) {
        if (!command.getCustomEngName().equals(this.customEngName)) {
            this.customEngName = command.getCustomEngName();
        }
    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getBankCode() {
        return bankCode;
    }

    public void setBankCode(String bankCode) {
        this.bankCode = bankCode;
    }

    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    public String getCustomEngName() {
        return customEngName;
    }

    public void setCustomEngName(String customEngName) {
        this.customEngName = customEngName;
    }

    public String getRegisterId() {
        return registerId;
    }

    public void setRegisterId(String registerId) {
        this.registerId = registerId;
    }

    public String getMandateId() {
        return mandateId;
    }

    public void setMandateId(String mandateId) {
        this.mandateId = mandateId;
    }

    public LocalDate getGrantStartDate() {
        return grantStartDate;
    }

    public void setGrantStartDate(LocalDate grantStartDate) {
        this.grantStartDate = grantStartDate;
    }

    public LocalDate getGrantExpiryDate() {
        return grantExpiryDate;
    }

    public void setGrantExpiryDate(LocalDate grantExpiryDate) {
        this.grantExpiryDate = grantExpiryDate;
    }

    public String getCreditReference() {
        return creditReference;
    }

    public void setCreditReference(String creditReference) {
        this.creditReference = creditReference;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public boolean finished() {
        return this.status != null && this.status.equals(EddaBindStatus.SUCCESS.value());
    }

    public void rebind(boolean isHsbc) {
        this.status = isHsbc ? EddaBindStatus.INIT.value() : EddaBindStatus.PROCESSING.value();
        this.registerId = "";
        this.mandateId = "";
        this.grantStartDate = null;
        this.grantExpiryDate = null;
        this.note = "";
    }

    public void finish(EddaApply eddaApply) {
        this.creditReference = eddaApply.getCreditReference();
        this.registerId = eddaApply.getRegisterId();
        this.mandateId = eddaApply.getMandateId();
        this.status = EddaBindStatus.SUCCESS.value();
        this.grantStartDate = eddaApply.getGrantStartDate();
        this.grantExpiryDate = eddaApply.getGrantExpiryDate();
        this.customEngName = eddaApply.getCustomEngName();
    }

    public void fail(EddaApply eddaApply) {
        this.creditReference = eddaApply.getCreditReference();
        this.registerId = eddaApply.getRegisterId();
        this.mandateId = eddaApply.getMandateId();
        this.status = EddaBindStatus.INVALID.value();
    }

}
